GATE Exam  >  GATE Questions  >  Consider the code segmentint i, j, x, y, m, n... Start Learning for Free
Consider the code segment
int i, j, x, y, m, n;
n = 20;
for (i = 0, i < n;="" />
{
for (j = 0; j < n;="" />
{
if (i % 2)
{
x + = ((4*j) + 5*i);
}
}
}
m = x + y;
Which one of the following is false?
  • a)
    The code contains loop invariant computation
  • b)
    There is scope of common subexpression elimination in this code
  • c)
    There is scope of strength reduction in this code
  • d)
    There is scope of dead code elimination in this code
Correct answer is option 'D'. Can you explain this answer?
Verified Answer
Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {fo...
int i,j,x,y,m,n;
N = 20;
for(i = 0; i < n;="" />
{
for(j = 0;j < n;="" />
{
if(i%2)
{
x + = 4*j[4
y + = (7+4*j);
}
}
}
M = x + y;
View all questions of this test
Most Upvoted Answer
Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {fo...
Invariants in the code:
- Invariants are expressions that remain true throughout the execution of a loop. They can be used to optimize code by eliminating redundant computations. In this code segment, there is one invariant computation: `x = ((4 * j) - (5 * i))`.

Common Subexpression Elimination:
- Common subexpression elimination is a compiler optimization technique that eliminates redundant computations by reusing previously computed values. In this code segment, there is scope for common subexpression elimination for the computation of `x = ((4 * j) - (5 * i))` because this computation is repeated in the inner loop for different values of `j` and `i`.

Strength Reduction:
- Strength reduction is another compiler optimization technique that replaces expensive operations with cheaper ones. It aims to reduce the number of operations and improve performance. In this code segment, there is scope for strength reduction for the computation of `x = ((4 * j) - (5 * i))`. The multiplication operations can be replaced with addition and subtraction operations to make the computation faster.

Dead Code Elimination:
- Dead code elimination is a compiler optimization technique that removes code that has no effect on the program's output. It eliminates unnecessary computations and improves performance. In this code segment, there is no scope for dead code elimination as all the computations are necessary for the final result.

Explanation of False Option:
The false option is option 'D': "There is scope of dead code elimination in this code."

- The code segment does not contain any dead code. All the computations are necessary for the final result.
- The code segment contains loop invariant computation, which is `x = ((4 * j) - (5 * i))`.
- There is scope for common subexpression elimination in this code segment as the same computation is repeated in the inner loop.
- There is scope for strength reduction in this code segment as the multiplication operations can be replaced with addition and subtraction operations.

Therefore, the false option is option 'D'.
Explore Courses for GATE exam
Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer?
Question Description
Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer?.
Solutions for Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer?, a detailed solution for Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer? has been provided alongside types of Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the code segmentint i, j, x, y, m, n;n = 20;for (i = 0, i {for (j = 0; j {if (i % 2){x + = ((4*j) + 5*i);}}}m = x + y;Which one of the following is false?a)The code contains loop invariant computationb)There is scope of common subexpression elimination in this codec)There is scope of strength reduction in this coded)There is scope of dead code elimination in this codeCorrect answer is option 'D'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev